local MOD_NAME = "mymod"
AddModRPCHandler(MOD_NAME, "reborn", function(player)
    if player:HasTag("playerghost") then
        player:PushEvent("respawnfromghost")
    end
end)
AddClientModRPCHandler(MOD_NAME, "ms_becameghost", function(player)
    player.HUD.rebornbtn:Show()
end)
local TEMPLATES = require "widgets/redux/templates"
AddClassPostConstruct("screens/playerhud", function(self)
    local _CreateOverlays = self.CreateOverlays
    function self:CreateOverlays(owner)
        _CreateOverlays(self, owner)
        self.rebornbtn = self:AddChild(TEMPLATES.StandardButton(function()
            SendModRPCToServer(MOD_RPC[MOD_NAME]["reborn"])
            self.rebornbtn:Hide()
        end, "我要复活", { 100, 50 }))
        -- 修改锚点为右上角
        self.rebornbtn:SetVAnchor(ANCHOR_TOP)
        self.rebornbtn:SetHAnchor(ANCHOR_RIGHT)
        -- 设置偏移量，向内偏移一些距离
        self.rebornbtn:SetPosition(-350, -65, 0)
        self.rebornbtn:SetScaleMode(SCALEMODE_PROPORTIONAL)
        self.rebornbtn:SetMaxPropUpscale(MAX_HUD_SCALE)
        self.owner:DoTaskInTime(0, function(inst)
            if self.owner:HasTag("playerghost") then
                self.rebornbtn:Show()
            else
                self.rebornbtn:Hide()
            end
        end)
    end
end)
-- ...existing code...

AddPlayerPostInit(function(inst)
    if not TheWorld.ismastersim then return inst end

    inst:DoTaskInTime(0, function(inst)
        if inst.components.inventory then
            -- 保存原始的 DropEverything 函数
            local old_DropEverything = inst.components.inventory.DropEverything

            -- 重写 DropEverything 函数
            inst.components.inventory.DropEverything = function(self, ondeath, keepequip)
                -- 如果是死亡触发且有不掉落标记，直接返回
                if ondeath then
                    return
                end
                -- 其他情况使用原始函数
                return old_DropEverything(self, ondeath, keepequip)
            end

            -- 以防万一，还是调用一下这些函数
            inst.components.inventory.dropondeath = false
            inst.components.inventory:DisableDropOnDeath()
        end

        -- 保持原有的鬼魂状态监听
        inst:ListenForEvent("ms_becameghost", function(inst)
            SendModRPCToClient(CLIENT_MOD_RPC[MOD_NAME]["ms_becameghost"], inst.userid, inst)
        end)
    end)

    -- 监听新的 inventory 组件添加（以防组件重新创建）
    inst:ListenForEvent("newcomponent", function(inst, data)
        if data.name == "inventory" then
            local old_DropEverything = inst.components.inventory.DropEverything
            inst.components.inventory.DropEverything = function(self, ondeath, keepequip)
                if ondeath then
                    return
                end
                return old_DropEverything(self, ondeath, keepequip)
            end
        end
    end)
end)
